﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Springwater.Business.Excel;
using Springwater.Business.Import;

namespace Springwater.Web.Controllers
{
    [Authorize(Roles="Admin")]
    public class ImportController : BaseController
    {

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Upload()
        {
            var file = Request.Files[0];

            if (file != null && file.ContentLength > 0)
            {
                List<string> errorList = new List<string>();
                var listDictionary = ExcelDataReader.Read(file.InputStream, errorList);
                BorvizImport borvizImport = new BorvizImport(this.UnitOfWork);
                ImportStatistic importStatistic = new ImportStatistic();

                if (errorList == null || errorList.Count == 0)
                {
                    errorList = borvizImport.Process(listDictionary, ref importStatistic);
                }

                if (errorList == null || errorList.Count == 0)
                {
                    return View("UploadSuccess", importStatistic);
                }
                else
                {
                    return View("UploadError", errorList);
                }
            }

            return RedirectToAction("UploadSuccess");
        }

        public ActionResult UploadSuccess()
        {
            return View();
        }

    }
}
